package com.lpy.book.JFrame;

import com.lpy.book.model.Book;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class UserMenuFrm extends JFrame {
    private List<Book> bookList;

    public UserMenuFrm() {
        initializeData();

        setTitle("图书管理系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        BackgroundPanel mainPanel = new BackgroundPanel();
        mainPanel.setLayout(new BorderLayout());

        String[] columnNames = {"书籍ID", "书名", "作者", "状态", "类型ID", "出版社", "库存", "价格", "描述"};
        Object[][] data = getBookData();
        DefaultTableModel tableModel = new DefaultTableModel(data, columnNames);
        JTable bookTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(bookTable);
        mainPanel.add(scrollPane, BorderLayout.CENTER);

        JPanel searchPanel = new JPanel();
        JTextField searchField = new JTextField(20);
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String keyword = searchField.getText();
                List<Book> searchResult = searchBooks(keyword);
                updateTableData(tableModel, searchResult);
            }
        });
        searchPanel.add(new JLabel("关键字:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);
        mainPanel.add(searchPanel, BorderLayout.NORTH);

        JPanel actionPanel = new JPanel();
        JButton borrowButton = new JButton("借书");
        borrowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                borrowBook(tableModel, bookTable);
            }
        });

        JButton returnButton = new JButton("还书");
        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                returnBook(tableModel, bookTable);
            }
        });

        actionPanel.add(borrowButton);
        actionPanel.add(returnButton);
        mainPanel.add(actionPanel, BorderLayout.SOUTH);

        setContentPane(mainPanel);

        setVisible(true);
    }

    private void borrowBook(DefaultTableModel model, JTable table) {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            Book selectedBook = bookList.get(selectedRow);
            if (selectedBook.getNumber() > 0) {
                selectedBook.setNumber(selectedBook.getNumber() - 1);
                if (selectedBook.getNumber() == 0) {
                    selectedBook.setStatus(2);
                }
                JOptionPane.showMessageDialog(UserMenuFrm.this, "借书成功！");
                updateTableData(model, bookList);
            } else {
                JOptionPane.showMessageDialog(UserMenuFrm.this, "库存不足，无法借阅！");
            }
        } else {
            JOptionPane.showMessageDialog(UserMenuFrm.this, "请选择要借阅的书籍！");
        }
    }

    private void returnBook(DefaultTableModel model, JTable table) {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            Book selectedBook = bookList.get(selectedRow);
            selectedBook.setNumber(selectedBook.getNumber() + 1);
            selectedBook.setStatus(1);
            JOptionPane.showMessageDialog(UserMenuFrm.this, "还书成功！");
            updateTableData(model, bookList);
        } else {
            JOptionPane.showMessageDialog(UserMenuFrm.this, "请选择要还的书籍！");
        }
    }

    private void initializeData() {
        bookList = new ArrayList<>();
        bookList.add(new Book(1, "书名1", "作者1", 1, 1, "出版社1", 10, 29.99, "描述1"));
        bookList.add(new Book(2, "书名2", "作者2", 1, 2, "出版社2", 5, 19.99, "描述2"));
        // 添加更多书籍...
    }

    private Object[][] getBookData() {
        Object[][] data = new Object[bookList.size()][9];
        for (int i = 0; i < bookList.size(); i++) {
            Book book = bookList.get(i);
            data[i] = new Object[]{book.getBookId(), book.getBookName(), book.getAuthor(),
                    book.getStatus(), book.getBookTypeId(), book.getPublish(),
                    book.getNumber(), book.getPrice(), book.getRemark()};
        }
        return data;
    }

    private List<Book> searchBooks(String keyword) {
        List<Book> result = new ArrayList<>();
        for (Book book : bookList) {
            if (book.getBookName().contains(keyword) || book.getAuthor().contains(keyword)) {
                result.add(book);
            }
        }
        return result;
    }

    private void updateTableData(DefaultTableModel model, List<Book> newData) {
        model.setRowCount(0);
        for (Book book : newData) {
            model.addRow(new Object[]{book.getBookId(), book.getBookName(), book.getAuthor(),
                    book.getStatus(), book.getBookTypeId(), book.getPublish(),
                    book.getNumber(), book.getPrice(), book.getRemark()});
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new UserMenuFrm();
            }
        });
    }

    class BackgroundPanel extends JPanel {
        private Image backgroundImage;

        public BackgroundPanel() {
            backgroundImage = new ImageIcon("path_to_your_image.jpg").getImage();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        }
    }
}


